package com.app.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.os.CountDownTimer;
import android.text.TextPaint;
import android.text.format.Time;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;


import java.sql.Date;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;

import lib.common.R;

/**
 * TODO: document your custom view class.
 */
public class MyOrderListTimeView extends TextView {



    public MyOrderListTimeView(Context context) {
        super(context);
        init(null, 0);

    }

    public MyOrderListTimeView(Context context, AttributeSet attrs) {
        super(context, attrs);

        init(attrs, 0);
    }

    public MyOrderListTimeView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        init(attrs, defStyle);
    }

    private void init(AttributeSet attrs, int defStyle) {
        // Load attributes

        if (null != attrs) {
            TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.MyOrderListTimeView);

            if (ta != null) {


               // ColorGroupCircle = ta.getColor(R.styleable.timeline_ColorRadiusBig, getResources().getColor(android.R.color.holo_green_light));


                ta.recycle();
            }
        }
        else{


        }


    }


    MyCountTimer CountTimer;
//    long timestamp ;
    public void start(long timestamp){
 //       this.timestamp = timestamp;

//        if(CountTimer != null){
//            CountTimer.cancel();
//        }

        if(CountTimer == null)
        {
            long time = timestamp - System.currentTimeMillis();
            CountTimer = new MyCountTimer(time, 1000);
            CountTimer.start();
        }

    }


    // http://www.2cto.com/kf/201207/139551.html
    class MyCountTimer extends CountDownTimer
    {

        long timestamp ;
        public MyCountTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);

            timestamp = millisInFuture;

        }

        @Override
        public void onTick(long l) {

            /*
            Date date_current = new Date(System.currentTimeMillis());
            Date date_close = new Date(timestamp);


            Calendar calendar_current = new GregorianCalendar();
            calendar_current.setTime(date_current);

            Calendar calendar_close = new GregorianCalendar();
            calendar_close.setTime(date_close);


            int day = calendar_close.get(Calendar.DAY_OF_MONTH) - calendar_current.get(Calendar.DAY_OF_MONTH);
            int hour = calendar_close.get(Calendar.HOUR_OF_DAY) - calendar_current.get(Calendar.HOUR_OF_DAY);
            int minutes = calendar_close.get(Calendar.MINUTE) - calendar_current.get(Calendar.MINUTE);
            int second = calendar_close.get(Calendar.SECOND) - calendar_current.get(Calendar.SECOND);
            */

            /*
            Time time_current = new Time();
            Time time_close = new Time();

            time_current.set(System.currentTimeMillis());
            time_close.set(timestamp);

            int day = time_close.yearDay - time_current.yearDay;
            int hour = time_close.hour - time_current.hour;
            int minutes = time_close.minute - time_current.minute;
            int second = time_close.second - time_current.second;
            */

            // 总时间设置每秒倒计时； list更新后导致View重新生成；

            Time time_current = new Time(Time.getCurrentTimezone());
            time_current.set(l);

            Time date_system_current = new Time(Time.getCurrentTimezone());
            date_system_current.set(System.currentTimeMillis());

            int day = time_current.monthDay - date_system_current.monthDay;
            int hour = time_current.hour;
            int minutes = time_current.minute;
            int second = time_current.second;


            /*
            if(day > 0){
                if(hour > 0){
                    if(minutes > 0){
                        if(second < 0)
                            second = 0;
                    }
                    else{
                        second = 0;
                    }

                }
                else{
                    minutes = 0;
                    second = 0;
                }
            }
            else{
                day = 0;
                hour = 0;
                minutes = 0;
                second = 0;
            }
            */
            /*
            if(hour<0)
                hour = 0;
            if(minutes<0)
                minutes = 0;
            if(second<0)
                second = 0;
            */

            String str = "";
            if(day > 0){
                str = "剩余"+day+"天"+hour+"小时"+minutes+"分"+second+"秒";
            }
            else{
                str = "剩余"+hour+"小时"+minutes+"分"+second+"秒";
            }

            setText(str);
        }

        @Override
        public void onFinish() {

        }
    }


}
